import java.io.*;

public class kmeans1 {
    static double[][] cluster = {{1, 1}, {2, 2}, {3, 3}};
    static double[][] point = new double[2000][2];
    static int[] kind = new int[2000];
    static int count = 0;
    static int[] Count = new int[3];

    public static void readfile() throws IOException {
        BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream("d:/data.txt"), "GBK"));

        String str = null;
        while ((str = in.readLine()) != null) {
            //System.out.println(str);
            //写入相关文件
            //out.write(str);
            //out.newLine();
            double point_x = Double.valueOf(str.split(",")[0]);
            double point_y = Double.valueOf(str.split(",")[1]);
            point[count][0] = point_x;
            point[count][1] = point_y;
            if (count < 3) {
                cluster[count][0] = point_x;
                cluster[count][1] = point_y;
            }
            count++;
        }
        //清楚缓存
        //out.flush();
        //关闭流
        in.close();
        //out.close();
    }

    public static void iter() {
        for (int i = 0; i < 20; i++) {

            for (int j = 0; j < 2000; j++) {
                double distance = 1000000000.0;
                for (int k = 0; k < 3; k++) {
                    double temp = Math.pow(point[j][0] - cluster[k][0], 2) + Math.pow(point[j][1] - cluster[k][1], 2);
                    if (temp < distance) {
                        distance = temp;
                        kind[j] = k;

                    }

                }
                //System.out.println(kind[j]);
                Count[kind[j]]++;
            }
            double[][] clus = new double[3][2];
            for (int j = 0; j < 2000; j++) {
                clus[kind[j]][0] += point[j][0];
                clus[kind[j]][1] += point[j][1];
            }
            for (int j = 0; j < 3; j++) {
                cluster[j][0] = clus[j][0] / (Count[j]);
                cluster[j][1] = clus[j][1] / (Count[j]);
                Count[j] = 0;

                System.out.println(cluster[j][0] + "," + cluster[j][1]);
            }
        }
    }

    public static void main(String[] args) throws IOException {
        readfile();
        iter();
        BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("/usr/local/hadoop/res.txt"), "GBK"));
        for (int i = 0; i < 2000; i++) {
            System.out.println(kind[i] + "\t" + point[i][0] + "," + point[i][1]);
            out.write(kind[i] + "\t" + point[i][0] + "," + point[i][1] + "\n");
        }
        out.flush();
        out.close();
    }
}